<?php

class Page_Handler {
  private $baseUrl  = 'https://minplan.abo.fi/minplan/';
  private $lastPage = '';
  private $page     = '';
  private $pageOpts = array();

  function __construct() {
    /**
     * Get last page information from session
     */
    $last_page = isset($_SESSION['last_page']) ? $_SESSION['last_page'] : null;
    if (strlen($last_page) > 0) {
      $this->lastPage = $last_page;
    }
  }

  function login($username, $password) {
    // Set starting page to 'login.do'
    $this->page     = 'login.do';
    $this->pageOpts = array(
      'actionDispatch'            => 'login_view_dologin',
      'selectedUserLoginName'     => $username,
      'selectedUserLoginPassword' => $password,
    );

    return $this->execute();
  }

  function logout() {
    $this->pageOpts = array('actionDispatch' => '%2Fga_logout.do');

    return $this->execute();
  }

  function myInformation() {
    $this->pageOpts = array(
      'actionDispatch' => '%2Fga_moduletab.do',
      'identifier'     => 'information',
    );

    $this->execute();
  }

  function studyPlan() {
  }

  function studyHandbook() {
  }

  function teacherTutor() {
  }

  function myRegistrations() {
    $this->pageOpts = array(
      'actionDispatch' => '%2Fga_moduletab.do',
      'identifier'     => 'my_registrations',
    );

    return $this->execute();
  }

  function myCourses() {
    $this->myRegistrations();

    $this->pageOpts = array(
      'actionDispatch' => 'my_registrations_view_tab',
      'identifier'     => 'my_registrations_my_course_registrations_tab'
    );

    return $this->execute();
  }

  function myExaminations() {
    $this->myRegistrations();

    $this->pageOpts = array(
      'actionDispatch' => 'my_registrations_view_tab',
      'identifier'     => 'my_registrations_my_examination_registrations_tab',
    );

    return $this->execute();
  }

  function myClassExaminations() {
    $this->myRegistrations();

    $this->pageOpts = array(
      'actionDispatch' => 'my_registrations_view_tab',
      'identifier'     => 'my_registrations_class_based_examinations',
    );

    return $this->execute();
  }

  function searchCourse($search_opts = array()) {
    $this->myCourses();

    $this->pageOpts = array('actionDispatch' => 'my_registrations_my_course_registrations_add_course_to_lov');
    $this->execute();

    return $this->searchCourseOrExamination($search_opts);
  }

  function searchExamination($search_opts = array()) {
    $this->myExaminations();

    $this->pageOpts = array('actionDispatch' => 'my_registrations_my_examination_registrations_add_course_to_lov');
    $this->execute();

    return $this->searchCourseOrExamination($search_opts);
  }

  private function searchCourseOrExamination($search_opts = array()) {
    /**
     * Förklaringar över search_opts:
     *
     * Kurskod     courseCode	                          = 000000.0
     * Fakultet    courseSearchCriteria.faculty	        = [0-9]
     * Andra språk courseSearchCriteria.foreignLanguage	= [on|false]
     * Kursnamn    courseSearchCriteria.freetext        = [string]
     * Handikapp   courseSearchCriteria.handicap        = [on|false]
     * Nätbaserad  courseSearchCriteria.netBased        = [on|false]
     * Period      courseSearchCriteria.period          = [0-9]
     * Utbildning  courseSearchCriteria.program	        = 1184
     * Typ         courseSearchCriteria.studyType       = [0-9]
     * Ämne        courseSearchCriteria.subject         = [0-9]
     * Läsår       courseSearchCriteria.yearCode        = 20092010
     * Årskurs     courseSearchCriteria.yearCourse      = [0-9]
     *
     * // Unik för tentamina
     * Tillstånd   selectedCourseSearchStateScope       = [active|active_and_archived]
     */
    $this->pageOpts = array('actionDispatch' => 'course_lov_view_search');

    if (sizeof($search_opts) > 0) {
      foreach ($search_opts as $key => $value) {
        $this->pageOpts[$key] = $value;
      }
    }

    return $this->execute();
  }

  function addCourse() {
  }

  function addExamination() {
  }

  function getCourse($course_id) {
    $this->myCourses();

    $this->pageOpts = array(
      'actionDispatch' => 'my_registrations_my_course_registrations_ejb_goto',
      'identifier'     => $course_id
    );

    return $this->execute();
  }

  function getCurrentPage() {
    return $this->lastPage;
  }


  /**
   * Execute a request for an url and fetch the data
   */
  private function execute() {
    global $fetcher;

    $url = $this->constructUrl();

    $data = $fetcher->fetch($url);
    $this->setLastPage($data);

    return $data;
  }

  private function constructUrl() {
    $page      = $this->page;
    $page_opts = $this->pageOpts;

    $this->page = '';
    $this->pageOpts = array();

    if (strlen($page) < 1) {
      $page = $this->lastPage;
    }

    $url  = $this->baseUrl;
    $url .= $page . '?';

    if (sizeof($page_opts) < 1) {
      return $url;
    }
    foreach ($page_opts as $id => $value) {
      $url .= "&{$id}={$value}";
    }

    return $url;
  }

  private function setLastPage(&$html) {
    $page = xPathify($html, '/html/body/form');
    $current_page = $page->item(0)->getAttributeNode('action')->nodeValue;
    
    // Remove '/minplan/' from current page
    $current_page = substr($current_page, 9);
    $current_page = explode(';', $current_page);
    $current_page = $current_page[0];

    $this->lastPage = $current_page;
    $_SESSION['last_page'] = $current_page;
  }
}

